package cn.xiongxf.web.controller;

import cn.xiongxf.doc.CourseDoc;
import cn.xiongxf.dto.ESCourseDto;
import cn.xiongxf.result.JsonResult;
import cn.xiongxf.result.PageList;
import cn.xiongxf.service.IESCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/es/course")
public class ESCourseController {
    @Autowired
    private IESCourseService esCourseService;


    /**
     * 保存课程到ES
     * @param doc
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('es:saveCourse')")
    public JsonResult saveCourseDoc(@RequestBody CourseDoc doc){
        esCourseService.save(doc);
        return JsonResult.success();
    }

    /**
     * 删除课程
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public JsonResult deleteCourseDoc(@PathVariable Long id){
        esCourseService.delete(id);
        return JsonResult.success();
    }

    /**
     * ES全局搜索课程
     * @param dto
     * @return
     */
    @PostMapping("/search")
    public JsonResult search(@RequestBody ESCourseDto dto) {
        PageList<CourseDoc> courseDocPageList = esCourseService.searchCourse(dto);
        return JsonResult.success(courseDocPageList);
    }
}
